package com.example.cursorsearch;

import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.example.cursorsearch.R;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View.OnClickListener;
import android.widget.*;

 public class MainActivity extends Activity  {
	 
	 
	 ArrayList<String> items = new ArrayList<String>();
	 Intent intent;
	 ArrayAdapter<String> adapter;
	// EditText editText1;
	 EditText edit_search;
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.activity_main);
		 
		// Button btn = (Button)findViewById(R.id.button1);
		//btn.setOnClickListener(this);
		 
		 
		 try {
			 String serviceUrl = "http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList";
			 String serviceKey = "OKJLjAtvWjTEGvHi7Iyzmx7LhXqLrGQaGdnVV75FkZ4ZiRNrOAw61cW5sayWE9Q3xFt989joOdoGI/30Ousbtw=="; // OpenAPI Key
             serviceKey = URLEncoder.encode(serviceKey);
             
             
             
             edit_search = (EditText) findViewById(R.id.editText2);
             String strSrch = edit_search.getText().toString(); 
             String strURL = serviceUrl + "?ServiceKey=" + serviceKey + "&strSrch=" + strSrch;
             
             URL url = new URL(strURL);
             
             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
             XmlPullParser parser = factory.newPullParser();
             parser.setInput(url.openStream(), "utf-8");
             
             int eventType = parser.getEventType();
             
             boolean bSet = false;
             
             while (eventType != XmlPullParser.END_DOCUMENT) {
            	 switch (eventType) {
            	    case XmlPullParser.START_DOCUMENT:
            	    	break;
            	    	
            	    case XmlPullParser.START_TAG:
            	    	String tag = parser.getName();
            	    	if (tag.equals("busRouteNm")/* || tag.equals("stStationNm") || tag.equals("edStationNm")*/) {
            	    		bSet = true;
            	    	}
            	    	break;
            	    	
            	    case XmlPullParser.END_TAG:
            	    	break;
            	    	
            	    case XmlPullParser.TEXT:
            	    	if (bSet) {
            	    		String content = parser.getText();
            	    		items.add(content);
            	    		//editText1.append(content + "\n");
            	    	
            	    		
            	    		
            	     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
            	    	
            	   	   ListView list = (ListView)findViewById(R.id.listView1);
            	   	   list.setAdapter(adapter);
            	   	   
            	   	   
            	   	   
            	   	edit_search.addTextChangedListener(new TextWatcher() {
            			
            			@Override
            			public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            				// When user changed the Text
            				MainActivity.this.adapter.getFilter().filter(cs);	
            			}
            			
            			@Override
            			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            					int arg3) {
            				// TODO Auto-generated method stub
            				
            			}
            			
            			@Override
            			public void afterTextChanged(Editable arg0) {
            				// TODO Auto-generated method stub							
            			}
            		});
                
                
            	   		   
            	   	   list.setOnItemClickListener(new OnItemClickListener() {	 
        				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        					String number = (String) parent.getItemAtPosition(position);
        					intent=new Intent(MainActivity.this, SubActivity.class);
        					
        					intent.putExtra("num", number);
        					
        					startActivity(intent);
        				}
        			});
            	   
            	   	   
            	    		bSet = false;
            	    	}
            	    	break;
            	    	
            	    case XmlPullParser.END_DOCUMENT:
            	    	
            	    	break;
            	 }
            	 
            	 eventType = parser.next();
            	 
             }
             
         	
		 } catch (Exception e) {
			// Toast.makeText(getContext(), e.getMessage(), 0).show();
		 }
	 }}
		 
		 
		  
	 

	 /*
	 public void onClick(View v) {
 
 
		 
		 
		  //editText1 = (EditText)findViewById(R.id.editText1);
	 
	 
		 
		 try {
			 String serviceUrl = "http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList";
			 String serviceKey = "OKJLjAtvWjTEGvHi7Iyzmx7LhXqLrGQaGdnVV75FkZ4ZiRNrOAw61cW5sayWE9Q3xFt989joOdoGI/30Ousbtw=="; // OpenAPI Key
             serviceKey = URLEncoder.encode(serviceKey);
             
             
             
             edit_search = (EditText) findViewById(R.id.editText2);
             String strSrch = edit_search.getText().toString(); 
             String strURL = serviceUrl + "?ServiceKey=" + serviceKey + "&strSrch=" + strSrch;
             
             URL url = new URL(strURL);
             
             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
             XmlPullParser parser = factory.newPullParser();
             parser.setInput(url.openStream(), "utf-8");
             
             int eventType = parser.getEventType();
             
             boolean bSet = false;
             
             while (eventType != XmlPullParser.END_DOCUMENT) {
            	 switch (eventType) {
            	    case XmlPullParser.START_DOCUMENT:
            	    	break;
            	    	
            	    case XmlPullParser.START_TAG:
            	    	String tag = parser.getName();
            	    	if (tag.equals("busRouteNm") //|| tag.equals("stStationNm") || tag.equals("edStationNm")) {
            	    		bSet = true;
            	    	}
            	    	break;
            	    	
            	    case XmlPullParser.END_TAG:
            	    	break;
            	    	
            	    case XmlPullParser.TEXT:
            	    	if (bSet) {
            	    		String content = parser.getText();
            	    		items.add(content);
            	    		//editText1.append(content + "\n");
            	    	
            	    		
            	    		
            	     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
            	    	
            	   	   ListView list = (ListView)findViewById(R.id.listView1);
            	   	   list.setAdapter(adapter);
            	   	   
            	   	   
            	   	   
            	   	edit_search.addTextChangedListener(new TextWatcher() {
            			
            			@Override
            			public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            				// When user changed the Text
            				MainActivity.this.adapter.getFilter().filter(cs);	
            			}
            			
            			@Override
            			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            					int arg3) {
            				// TODO Auto-generated method stub
            				
            			}
            			
            			@Override
            			public void afterTextChanged(Editable arg0) {
            				// TODO Auto-generated method stub							
            			}
            		});
                
                
            	   		   
            	   	   list.setOnItemClickListener(new OnItemClickListener() {	 
        				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        					String number = (String) parent.getItemAtPosition(position);
        					intent=new Intent(MainActivity.this, SubActivity.class);
        					
        					intent.putExtra("num", number);
        					
        					startActivity(intent);
        				}
        			});
            	   
            	   	   
            	    		bSet = false;
            	    	}
            	    	break;
            	    	
            	    case XmlPullParser.END_DOCUMENT:
            	    	
            	    	break;
            	 }
            	 
            	 eventType = parser.next();
            	 
             }
             
         	
		 } catch (Exception e) {
			 Toast.makeText(v.getContext(), e.getMessage(), 0).show();
		 }
		 
		 
		  
	 }*/
 
 
 
	 